package com.lbs.tdrive.activity;


import java.util.List;

import com.lbs.tdrive.data.Vehicle;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lbs.tdrive.comon.HttpASync.OnRequest;
import com.lbs.tdrive.R;
import com.lbs.tdrive.comon.HttpASync;
import com.lbs.tdrive.constant.AppConstant;
import com.lbs.view.adapter.VehicleAdapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

public class MyVehicles extends Activity implements OnClickListener,OnRequest{
	
     private ImageView add;
     private ListView list;
     private ImageView delete;
     private ImageView edit;
     private VehicleAdapter vehicleAdapter;
 	public static final int DELETE_VEHICLE=1;
 	private Context ctx;
	private Handler handler=new Handler(){
		

		public void handleMessage(android.os.Message msg) {
			if(msg.what==MyVehicles.DELETE_VEHICLE) {
				
				Toast.makeText(ctx, "Delete", Toast.LENGTH_SHORT).show();
				
			}
		};
	};
	
	 protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.vehicles);
	        ctx = this;
	        add=(ImageView)findViewById(R.id.imageView3);
	        list=(ListView)findViewById(R.id.listView1);
	        add.setOnClickListener((OnClickListener) this);
	        
	 }
	 
	 public void onClick(View v) {
	    	if(v.getId()==R.id.imageView3){
	    	
	    	  Intent intent = new Intent(ctx,AddVehicle.class);
	    	  startActivity(intent);
	    	  
	    	}
	    	else if (v.getId() == R.id.imageView1) {
	 			Intent intent = new Intent(ctx, EditVehicle.class);
				startActivity(intent);
			}
	    	

		}
		@Override
		public void onBackPressed() {
			// TODO Auto-generated method stub
			finish();
		}

	 
	 @Override
		protected void onResume() {
			// TODO Auto-generated method stub
			super.onResume();
			SharedPreferences pref = getSharedPreferences(AppConstant.PREFS_NAME,0);
			String uName = pref.getString("username", null);
			HttpASync async = new HttpASync();
			String url=AppConstant.ADD_VEHICLE+"?emailid="+uName+"&method=get";
			async.setOnRequestListener(this);
			async.execute(url);
		}
	 
	 @Override
		public void onResponseReceived(String response) {
			
			Gson gson = new Gson();
			
			final List<Vehicle> vehicleListData = gson.fromJson(response,new TypeToken<List<Vehicle>>(){}.getType());
			for (Vehicle vehicle : vehicleListData) {
				System.out.println("Vehicle : "+vehicle.getVehicleName());
			}
			runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					vehicleAdapter=new VehicleAdapter(ctx,handler,vehicleListData);
					list.setAdapter(vehicleAdapter);
				}
			});
			
		}
}
